package com.lucker.controller.bettype;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lucker.common.model.BetAIPlanObj;
import com.lucker.common.model.BetListRequestMode;
import com.lucker.common.model.BetObj;
import com.lucker.common.model.BetRequestMode;
import com.lucker.common.model.BetTargetObj;
import com.lucker.common.util.StringUtil;
import com.lucker.controller.BaseController;
import com.lucker.framework.constant.ResponseBaseModel;
import com.lucker.framework.filter.BodyReaderHttpServletRequestWrapper;
import com.lucker.service.caipiaosimulationplan.CaiPiaoSimulationPlanService;

@Controller
@RequestMapping(value = "bettype")
public class BetTypeController extends BaseController {
	
	@Autowired
	CaiPiaoSimulationPlanService caiPiaoSimulationPlanService;
	
	@RequestMapping(value="commit",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> commit(HttpServletRequest req) {
		try {
			BodyReaderHttpServletRequestWrapper requestWrapper = new BodyReaderHttpServletRequestWrapper(req);
            String body = requestWrapper.getBody();
			
            JSONObject jsonObject = JSONObject.parseObject(body);
            
			
			String _str_betTargetObjs = jsonObject.getString("betTargetObjs");
			if(StringUtil.isBlank(_str_betTargetObjs)) return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少betTargetObjs！"), HttpStatus.OK);
			String _str_betAIPlanObjs = jsonObject.getString("betAIPlanObjs");
			if(StringUtil.isBlank(_str_betAIPlanObjs)) return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少betAIPlanObjs！"), HttpStatus.OK);
//			
			List<BetTargetObj> betTargetObjs = JSONObject.parseArray(_str_betTargetObjs, BetTargetObj.class);
			List<BetAIPlanObj> betAIPlanObjs = JSONObject.parseArray(_str_betAIPlanObjs, BetAIPlanObj.class);
//			
			BetObj vo = new BetObj();
			vo.setBetAIPlanObjs(betAIPlanObjs);
			vo.setBetTargetObjs(betTargetObjs);
			getLoger().error("投注提交参数 :  ====================== {} ", JSONObject.toJSONString(vo));
			String userId = req.getHeader("userId");
			if(StringUtil.isBlank(userId)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少USERID！"), HttpStatus.OK);
			}
			String cai_id = req.getHeader("caiId");
			if(StringUtil.isBlank(cai_id)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少caiId！"), HttpStatus.OK);
			}
			
			return caiPiaoSimulationPlanService.bet(Long.valueOf(userId), Long.valueOf(cai_id), vo);
		}catch(Exception e) {
			this.getLoger().error("投注异常, 详情:  {} , {}", e.getMessage(),e);
			String msg = StringUtil.substring(e.getMessage(), 0, 150);
			
			return new ResponseEntity<>(new ResponseBaseModel(29002,msg), HttpStatus.OK);
		}
		
	}
	
	@RequestMapping(value="list",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> list(HttpServletRequest req,@RequestBody BetListRequestMode vo) {
		try {
			
			getLoger().error("查询投注列表提交参数:  ====================== {} ", JSONObject.toJSONString(vo));
			String userId = req.getHeader("userId");
			if(StringUtil.isBlank(userId)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少USERID！"), HttpStatus.OK);
			}
			
			return caiPiaoSimulationPlanService.getCaiPiaoSimulationPlanList(Long.valueOf(userId), Integer.parseInt(vo.getPagesize()), Integer.parseInt(vo.getCurpage()));
			
//			CaiPiaoSimulationPlan _plan = new CaiPiaoSimulationPlan();
//			_plan.setUserId(Long.valueOf(userId));
//			_plan.orderById(false);
//			
//			Page<CaiPiaoSimulationPlan> page =  PageHelper.startPage(1, 2);
//			caiPiaoSimulationPlanService.queryAll(_plan);
//			getLoger().error("查询结果==========================  {} ",JSONObject.toJSONString(page));
//			
//			return new ResponseEntity<>(new ResponseBaseModel(10000,"ok"), HttpStatus.OK);
			
		}catch(Exception e) {
			this.getLoger().error("查询投注列异常, 详情:  {} , {}", e.getMessage(),e);
			String msg = StringUtil.substring(e.getMessage(), 0, 150);
			
			return new ResponseEntity<>(new ResponseBaseModel(29002,msg), HttpStatus.OK);
		}
		
	}
	
	
	
	@RequestMapping(value="plandetail",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> plandetail(HttpServletRequest req,@RequestBody JSONObject vo) {
		try {
			
			getLoger().error("查询投注列表提交参数:  ====================== {} ", JSONObject.toJSONString(vo));
			String userId = req.getHeader("userId");
			if(StringUtil.isBlank(userId)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少USERID！"), HttpStatus.OK);
			}
			if(vo == null || StringUtil.isBlank(vo.getString("planId"))) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"请求参数缺少planId "), HttpStatus.OK);
			}
			
			return caiPiaoSimulationPlanService.getBetPlanDetailList(Long.valueOf(vo.getString("planId")));
		}catch(Exception e) {
			this.getLoger().error("获取投注明细错误, 详情:  {} , {}", e.getMessage(),e);
			String msg = StringUtil.substring(e.getMessage(), 0, 150);
			
			return new ResponseEntity<>(new ResponseBaseModel(29002,msg), HttpStatus.OK);
		}
		
	}
	
	
	

}
